package com.sigmazero13.gameutils.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class PlayReport {
	
	private Set<String> rowHeadings;
	private Set<String> colHeadings;
	
	private Map<String, Map<String, WinLossInfo>> report;
	
	public PlayReport() {
		report = new HashMap<String, Map<String, WinLossInfo>>();
		rowHeadings = new HashSet<String>();
		colHeadings = new HashSet<String>();
	}
	
	public List<String> getRowHeadings() {
		List<String> rows = new ArrayList<String>(rowHeadings);
		Collections.sort(rows);
		return rows;
	}
	
	public List<String> getColHeadings() {
		List<String> cols = new ArrayList<String>(colHeadings);
		Collections.sort(cols);
		return cols;
	}
	
	public WinLossInfo getCellInfo(String rowName, String colName) {
		if (report.containsKey(rowName)) {
			Map<String, WinLossInfo> row = report.get(rowName);
			if (row.containsKey(colName)) {
				return row.get(colName);
			}
		}
		
		return new WinLossInfo();
	}
	
	public void addResult(String rowKey, String colKey, boolean win) {
		if (rowKey == null || rowKey.equals("") || colKey == null || colKey.equals(""))
			return;
		
		if (!report.containsKey(rowKey)) {
			report.put(rowKey, new HashMap<String, WinLossInfo>());
			rowHeadings.add(rowKey);
		}
		Map<String, WinLossInfo> row = report.get(rowKey);
		
		if (!row.containsKey(colKey)) {
			row.put(colKey, new WinLossInfo());
			colHeadings.add(colKey);
		}
		WinLossInfo info = row.get(colKey);
		
		if (win) 
			info.addWin();
		else 
			info.addLoss();
	}
	
	public void clearResults() {
		report.clear();
		rowHeadings.clear();
		colHeadings.clear();
	}
}
